package com.smart.car.points.controller;

import com.smart.car.common.res.bean.ResponseResult;
import com.smart.car.common.res.constant.Constants;
import com.smart.car.common.res.exception.BusinessException;
import com.smart.car.points.annotation.RequestLimit;
import com.smart.car.points.entity.PointsInfo;
import com.smart.car.points.service.PointsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * TODO 用户积分控制
 * @author: zoutao
 * @date: 2021-01-20
 */
@Api(tags = "积分模块")
@Slf4j
@RestController
@RequestMapping("")
public class PointsInfoController {

    @ApiIgnore
    @GetMapping("/test")
    public String test() {
        log.info("hello-江湖一点雨");
        return "hello-world-江湖一点雨";
    }

    @Autowired
    private PointsInfoService infoService;

    //SPEL表达式
    @Value("${server.port}")
    private String port;

    /**
     * TODO 远程调用实例
     * @author zoutao.blog.csdn.net
     */
    @ApiOperation(value = "远程调用-查询用户积分")
    @GetMapping("/info/{id}")
    public ResponseResult getUserPoints(@PathVariable("id") String id ) {
        PointsInfo userPoint = new PointsInfo();
        userPoint.setMemberId(id);
        userPoint.setCurQty("100");
        userPoint.setCreateBy("江湖一点雨");
        ResponseResult result = new ResponseResult();
        //int a = 1/0;
        result.setData(userPoint);
        result.setCode(Constants.SUCCESS);
        result.setMsg("远程调用结果,来自于："+port);
        return result;
    }

    /**
     * TODO 开通会员增加积分记录
     * @author zoutao.blog.csdn.net
     */
    @RequestLimit(count = 3,time = 30L)
    @ApiOperation(value = "开通会员-增加积分")
    @PostMapping(value = "/addCard")
    public ResponseResult<Integer> addCard(String json) throws BusinessException {
        log.info("远程调用服务端：用户开通月度会员后，增加积分记录 = " + json);
        ResponseResult<Integer> result = new ResponseResult<>();
        int rtn = infoService.addMemberCard(json);
        result.setData(rtn);
        return result;
    }

    @RequestLimit(count = 3,time = 30L)
    @ApiOperation(value = "查询所有用户积分信息")
    @GetMapping("/list")
    public ResponseResult list() {
        List<PointsInfo> pointsInfos = infoService.list();
        log.debug("查询到的用户积分数据为： " + pointsInfos);
        return ResponseResult.ok(pointsInfos);
    }

    @ApiOperation(value = "更新用户积分")
    @PostMapping(value = "/updateCard")
    public ResponseResult<Integer> updateCard(String json) throws BusinessException {
        log.info("更新积分记录：" + json);
        ResponseResult<Integer> result = new ResponseResult<>();
        int rtn = infoService.updateMemberCard(json);
        result.setData(rtn);
        return result;
    }

    /**
     * TODO 获得用户积分数据
     * @author zoutao.blog.csdn.net
     */
    @ApiOperation(value = "获取用户积分整体数据")
    @PostMapping(value = "/getData")
    public ResponseResult<PointsInfo> getData(String memberId) throws BusinessException {
        ResponseResult<PointsInfo> result = new ResponseResult<>();
        PointsInfo rtn = infoService.getMemberCard(memberId);
        result.setData(rtn);
        return result;
    }

}
